package com.spogher.bsc.pages;

import java.io.Serializable;
import java.util.List;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxSubmitLink;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;

import com.spogher.bsc.BscSession;
import com.spogher.bsc.data.dao.interfaces.PlayerDao;
import com.spogher.bsc.data.dao.interfaces.TeamDao;
import com.spogher.bsc.data.dataobjects.Player;
import com.spogher.bsc.data.dataobjects.Team;
import com.spogher.components.dropdownChoices.PlayerDropDownChoice;
import com.spogher.components.dropdownChoices.TeamDropDownChoice;

public class ManagePlayerPage extends BscTemplatePage{

	private static final String SUCCESS_MESSAGE = "player successfully updated";
	private static final String ERROR_MESSAGE = "an error occured, please try again";
	
	@SpringBean
	private TeamDao teamDao;
	@SpringBean
	private PlayerDao playerDao;
	private String formFeedBackLabelMessage = "";

	public ManagePlayerPage() {
		final Label feedbackLabel = new Label("formFeedback", new Model<String>(formFeedBackLabelMessage));
		feedbackLabel.setOutputMarkupId(true);
		add(feedbackLabel);
		Form<ManagePlayerModelObject> managePlayerForm = new Form<ManagePlayerModelObject>("managePlayerForm", 
				new CompoundPropertyModel<ManagePlayerModelObject>(new ManagePlayerModelObject()));
		
		List<Team> teamList;
		List<Player> playerList;
		if (BscSession.get().isRootUser()) {
			teamList = teamDao.findAll();
			playerList = playerDao.findAll();
		}
		else {
			teamList = teamDao.findByManager(BscSession.get().getManager());
			playerList = playerDao.findByManager(BscSession.get().getManager());
		}
		
		DropDownChoice<Player> playerChoice = new PlayerDropDownChoice("player", playerList);
		managePlayerForm.add(playerChoice);
		
		DropDownChoice<Team> teamChoice = new TeamDropDownChoice("team", teamList);
		managePlayerForm.add(teamChoice);	
		
		managePlayerForm.add(new AjaxSubmitLink("submit"){
			private static final long serialVersionUID = 1L;

			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				ManagePlayerModelObject managePlayerModelObject = (ManagePlayerModelObject) form.getModelObject();
				managePlayerModelObject.getPlayer().setTeam(managePlayerModelObject.getTeam());
				playerDao.save(managePlayerModelObject.getPlayer());
				formFeedBackLabelMessage = SUCCESS_MESSAGE;
				feedbackLabel.setDefaultModel(new Model<String>(formFeedBackLabelMessage));
				target.addComponent(feedbackLabel);
			}
		});	
		add(managePlayerForm);

	}
	
	private class ManagePlayerModelObject implements Serializable{

		private static final long serialVersionUID = 1L;
		private Player player;
		private Team team;
		public void setPlayer(Player player) {
			this.player = player;
		}
		public Player getPlayer() {
			return player;
		}
		public void setTeam(Team team) {
			this.team = team;
		}
		public Team getTeam() {
			return team;
		}
		
	}
}
